/**
 * 
 * 起迪科技 Copyright (c) 2014-2018 QiDi,Inc.All Rights Reserved.
 */
package cn.qidisoft.edu.hzjt.service.impl;

import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.qidisoft.core.common.code.QdfRespCode;
import cn.qidisoft.core.common.exception.dao.DBException;
import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.core.common.pagination.Page;
import cn.qidisoft.core.common.pagination.PaginationBean;
import cn.qidisoft.edu.hzjt.dao.RecommendStudentMapper;
import cn.qidisoft.edu.hzjt.model.Grade;
import cn.qidisoft.edu.hzjt.model.RecommendStudent;
import cn.qidisoft.edu.hzjt.model.Semester;
import cn.qidisoft.edu.hzjt.model.Student;
import cn.qidisoft.edu.hzjt.service.IRecommendStudentService;
import cn.qidisoft.edu.hzjt.utils.LogType;

/**
 * 
 * @author hxq
 * @version $Id: RecommendStudentServiceImpl.java, v 0.1 2018年11月1日 上午10:12:02 hxq Exp $
 */
@Service("recommendStudentService")
public class RecommendStudentServiceImpl implements IRecommendStudentService {
  protected static final LoggerAdapter LOGGER =
      LoggerAdapterFactory.getLogger(LogType.HZJT_LOGGER.val);
  @Autowired
  private RecommendStudentMapper recommendStudentMapper;

  /**
   * @see cn.qidisoft.edu.hzjt.service.IRecommendStudentService#queryRecommendStudentPageList(cn.qidisoft.edu.hzjt.model.RecommendStudent,
   *      cn.qidisoft.core.common.pagination.Page)
   */
  @Override
  public PaginationBean<RecommendStudent> queryRecommendStudentPageList(
      RecommendStudent recommendStudent, Page page) {
    try {
      int c = recommendStudentMapper.queryRecommendStudentPageCount(recommendStudent);
      int totalRecords = 0;
      totalRecords = (int) c;
      PaginationBean<RecommendStudent> pageHolder =
          new PaginationBean<RecommendStudent>(page, totalRecords);
      List<RecommendStudent> resultList =
          recommendStudentMapper.queryRecommendStudentPageList(recommendStudent,
              new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                  pageHolder.getPageSize()));
      pageHolder.setPageList(resultList);
      return pageHolder;
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error(
          "queryRecommendStudentPageList [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(), ToStringBuilder.reflectionToString(
              recommendStudent + "===" + page, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IRecommendStudentService#addRecommendStudent(cn.qidisoft.edu.hzjt.model.RecommendStudent)
   */
  @Override
  @Transactional
  public int addRecommendStudent(RecommendStudent recommendStudent) {
    try {
      recommendStudentMapper.insert(recommendStudent);
      return recommendStudent.getTjid();
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("addRecommendStudent [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(recommendStudent, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IRecommendStudentService#queryGidRecommendStudent(java.lang.String)
   */
  @Override
  public RecommendStudent queryGidRecommendStudent(String gid) {
    try {
      return recommendStudentMapper.queryGidByPK(gid);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("queryGidRecommendStudent [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(gid, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IRecommendStudentService#queryRecommendStudent(java.lang.Integer)
   */
  @Override
  public RecommendStudent queryRecommendStudent(Integer tjid) {
    try {
      return recommendStudentMapper.queryByPK(tjid);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("queryRecommendStudent [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(tjid, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IRecommendStudentService#modiftyRecommendStudent(cn.qidisoft.edu.hzjt.model.RecommendStudent)
   */
  @Override
  @Transactional
  public int modiftyRecommendStudent(RecommendStudent recommendStudent) {
    try {
      return recommendStudentMapper.updateByPK(recommendStudent);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("modiftyRecommendStudent [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(recommendStudent, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IRecommendStudentService#removeRecommendStudent(java.lang.String[])
   */
  @Override
  @Transactional
  public int removeRecommendStudent(String[] ids) {
    int num = 0;
    try {
      for (String id : ids) {
        recommendStudentMapper.deleteByPK(Integer.parseInt(id));
        num++;
      }
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("removeRecommendStudent [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(ids, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
    return num;
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IRecommendStudentService#queryRecommendStudentClassCount(cn.qidisoft.edu.hzjt.model.RecommendStudent)
   */
  @Override
  public int queryRecommendStudentClassCount(RecommendStudent recommendStudent) {
    try {
      return recommendStudentMapper.queryRecommendStudentClassCount(recommendStudent);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error(
          "queryRecommendStudentClassCount [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(recommendStudent, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IRecommendStudentService#queryRecommendStudentAllTerm()
   */
  @Override
  public List<Semester> queryRecommendStudentAllTerm() {
    try {
      return recommendStudentMapper.queryRecommendStudentAllTerm();
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("queryRecommendStudentAllTerm [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(null, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IRecommendStudentService#queryRecommendStudentClassReport(cn.qidisoft.edu.hzjt.model.Student)
   */
  @Override
  public List<RecommendStudent> queryRecommendStudentClassReport(Student student) {
    try {
      return recommendStudentMapper.queryRecommendStudentClassReport(student);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error(
          "queryRecommendStudentClassReport [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(student, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IRecommendStudentService#queryRecommendStudentSchoolReport(cn.qidisoft.edu.hzjt.model.Student)
   */
  @Override
  public List<RecommendStudent> queryRecommendStudentSchoolReport(Student student) {
    try {
      return recommendStudentMapper.queryRecommendStudentSchoolReport(student);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error(
          "queryRecommendStudentSchoolReport [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(student, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IRecommendStudentService#queryRecommendStudentAllGrade()
   */
  @Override
  public List<Grade> queryRecommendStudentAllGrade(Grade grade) {
    try {
      return recommendStudentMapper.queryRecommendStudentAllGrade(grade);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error(
          "queryRecommendStudentAllGrade [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(null, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

  /**
   * @see cn.qidisoft.edu.hzjt.service.IRecommendStudentService#exportRecommendStudentList(cn.qidisoft.edu.hzjt.model.RecommendStudent)
   */
  @Override
  public List<RecommendStudent> exportRecommendStudentList(RecommendStudent recommendStudent) {
    try {
      return recommendStudentMapper.exportRecommendStudentList(recommendStudent);
    } catch (Exception e) {
      e.printStackTrace();
      LOGGER.error("exportRecommendStudentList [{}] error," + this.getClass().getName() + "=[{}]",
          this.getClass().getName(),
          ToStringBuilder.reflectionToString(recommendStudent, ToStringStyle.SHORT_PREFIX_STYLE));
      throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(), e);
    }
  }

}
